home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JToolBar.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  417 lines

  1. /*
  2.  * @(#)JToolBar.java    1.41 98/02/16
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.Color;
  24. import java.awt.Component;
  25. import java.awt.Dimension;
  26. import java.awt.Graphics;
  27. import java.awt.Insets;
  28. import java.awt.event.*;
  29. import java.beans.*;
  30. import java.io.Serializable;
  31.  
  32. import com.sun.java.swing.border.Border;
  33. import com.sun.java.swing.plaf.*;
  34. import com.sun.java.accessibility.*;
  35.  
  36. /**
  37.  * JToolBar provides a component which is useful for displaying commonly
  38.  * used Actions or controls.  It can be dragged out into a seperate window
  39.  * by the user (unless the floatable property is set to false).  In order
  40.  * for drag-out to work correctly, it is recommended that you add JToolBar
  41.  * instances to one of the four 'sides' of a container whose layout manager 
  42.  * is a BorderLayout, and do not add children to any of the other four 'sides'.
  43.  * <p>
  44.  * For the keyboard keys used by this component in the standard Look and
  45.  * Feel (L&F) renditions, see the
  46.  * <a href="doc-files/Key-Index.html#JToolBar">JToolBar</a> key assignments.
  47.  * <p>
  48.  * Warning: serialized objects of this class will not be compatible with
  49.  * future swing releases.  The current serialization support is appropriate
  50.  * for short term storage or RMI between Swing1.0 applications.  It will
  51.  * not be possible to load serialized Swing1.0 objects with future releases
  52.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  53.  * baseline for the serialized form of Swing objects.
  54.  *
  55.  * @version 1.41 02/16/98
  56.  * @author Georges Saab
  57.  * @see Action
  58.  */
  59. public class JToolBar extends JComponent implements Accessible {
  60.     private    boolean   paintBorder              = true;  
  61.     private    Insets    margin                   = null;
  62.     private    boolean   floatable                = true;
  63.  
  64.  
  65.     /**
  66.      * Create a new toolbar.
  67.      */
  68.     public JToolBar() {
  69.         // Create the Popup
  70.         this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  71.         updateUI();
  72.     }
  73.  
  74.     /**
  75.      * Returns the toolbar's current UI.
  76.      * @see setUI
  77.      */
  78.     public ToolBarUI getUI() {
  79.         return (ToolBarUI)ui;
  80.     }
  81.     
  82.     /**
  83.      * Sets the L&F object that renders this component.
  84.      *
  85.      * @param ui  the ToolBarUI L&F object
  86.      * @see UIDefaults#getUI
  87.      * @beaninfo
  88.      * description: The menu item's UI delegate
  89.      *       bound: true
  90.      *      expert: true
  91.      *      hidden: true
  92.      */
  93.     public void setUI(ToolBarUI ui) {
  94.         super.setUI(ui);
  95.     }
  96.     
  97.     /**
  98.      * Notification from the UIFactory that the L&F has changed. 
  99.      * Called to replace the UI with the latest version from the 
  100.      * UIFactory.
  101.      *
  102.      * @see JComponent#updateUI
  103.      */
  104.     public void updateUI() {
  105.         setUI((ToolBarUI)UIManager.getUI(this));
  106.         invalidate();
  107.     }
  108.  
  109.  
  110.  
  111.     /**
  112.      * Returns the name of the L&F class that renders this component.
  113.      *
  114.      * @return "ToolBarUI"
  115.      * @see JComponent#getUIClassID
  116.      * @see UIDefaults#getUI
  117.      */
  118.     public String getUIClassID() {
  119.         return "ToolBarUI";
  120.     }
  121.  
  122.  
  123.     /*
  124.      * Returns the index of the specified component.
  125.      * (Note: Separators occupy index positions.)
  126.      *
  127.      * @param c  the Component to find
  128.      * @return an int indicating the component's position, where 0=first
  129.      */
  130.     public int getComponentIndex(Component c) {
  131.         int ncomponents = this.getComponentCount();
  132.         Component[] component = this.getComponents();
  133.         for (int i = 0 ; i < ncomponents ; i++) {
  134.             Component comp = component[i];
  135.             if (comp == c) 
  136.                 return i;
  137.         }
  138.         return -1;
  139.     }
  140.  
  141.     /*
  142.      * Returns the component at the specified index.
  143.      *
  144.      * @param i  the component's position, where 0=first
  145.      * @return   the Component at that position, or null for an
  146.      *           invalid index or if there is a separator at that
  147.      *           position
  148.      */
  149.     public Component getComponentAtIndex(int i) {
  150.         int ncomponents = this.getComponentCount();
  151.         if (i <= ncomponents) {
  152.             Component[] component = this.getComponents();
  153.             return component[i];
  154.         }
  155.         return null;
  156.     }
  157.  
  158.      /**
  159.       * Sets the margin between the toolbar's border and
  160.       * its buttons. Setting to null causes the toolbar to
  161.       * use the default margins. The toolbar's default Border
  162.       * object uses this value to create the proper margin.
  163.       * However, if a non-default border is set on the toolbar, 
  164.       * it is that Border object's responsibility to create the
  165.       * appropriate margin space (otherwise this property will 
  166.       * effectively be ignored).
  167.       *
  168.       * @param m an Insets object that defines the space between the 
  169.       *          border and the buttons
  170.       * @see Insets
  171.       * @beaninfo
  172.       * description: The margin between the toolbar's border and contents
  173.       *       bound: true
  174.       *      expert: true
  175.       */
  176.      public void setMargin(Insets m) {
  177.          Insets old = margin;
  178.          margin = m;
  179.          firePropertyChange("margin", old, m);
  180.          invalidate();
  181.      }
  182.  
  183.      /**
  184.       * Returns the margin between the toolbar's border and
  185.       * its buttons.
  186.       *
  187.       * @return an Insets object containing the margin values
  188.       * @see Insets
  189.       */
  190.      public Insets getMargin() {
  191.          if(margin == null) {
  192.              return new Insets(0,0,0,0);
  193.          } else {
  194.              return margin;
  195.          }
  196.      }
  197.  
  198.      /**
  199.       * Checks whether the border should be painted.
  200.       *
  201.       * @return true if the border should be painted, else false
  202.       * @see setBorderPainted
  203.       */
  204.      public boolean isBorderPainted() {
  205.          return paintBorder;
  206.      }
  207.      
  208.  
  209.      /**
  210.       * Sets whether the border should be painted.
  211.       *
  212.       * @param b if true, the border is painted.
  213.       * @see isBorderPainted
  214.       * @beaninfo
  215.       * description: Does the toolbar paint its borders?
  216.       *      expert: true
  217.       */
  218.      public void setBorderPainted(boolean b) {
  219.          paintBorder = b;
  220.          invalidate();
  221.      }
  222.  
  223.      /**
  224.       * Paint the toolbar's border if BorderPainted property is true.
  225.       * 
  226.       * @param g  the Graphics context in which the painting is done
  227.       * @see JComponent#paint
  228.       * @see JComponent#setBorder
  229.       */
  230.      protected void paintBorder(Graphics g) {    
  231.          if (isBorderPainted()) {
  232.              super.paintBorder(g);
  233.          }
  234.      }
  235.  
  236.     /** 
  237.      * Return true if the Toolbar can be dragged out by the user.
  238.      *
  239.      * @return true if the Toolbar can be dragged out by the user
  240.      */
  241.     public boolean isFloatable() {
  242.         return floatable;
  243.     }
  244.  
  245.      /**
  246.       * Sets whether the toolbar can be made to float
  247.       *
  248.       * @param b if true, the toolbar can be dragged out
  249.       * @see isFloatable
  250.       * @beaninfo
  251.       * description: Can the toolbar be made to float by the user?
  252.       *       bound: true
  253.       *   preferred: true
  254.       */
  255.     public void setFloatable(boolean b) {
  256.         if (floatable != b) {
  257.             this.floatable = b;
  258.             firePropertyChange("floatable", !floatable, floatable);         
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * Appends a toolbar-specific separator to the end of the toolbar
  264.      * (not a JSeparator).
  265.      */
  266.     public void addSeparator() {
  267.         JToolBar.Separator s = new JToolBar.Separator();
  268.         add(s);
  269.     }
  270.  
  271.     /**
  272.      * Add a new JButton which dispatches the action.
  273.      *
  274.      * @param a the Action object to add as a new menu item
  275.      */
  276.     public JButton add(Action a) {
  277.         JButton b = new JButton((String)a.getValue(Action.NAME),
  278.                                 (Icon)a.getValue(Action.SMALL_ICON));
  279.         b.setHorizontalTextPosition(JButton.CENTER);
  280.         b.setVerticalTextPosition(JButton.BOTTOM);
  281.         b.setEnabled(a.isEnabled());
  282.         b.addActionListener(a);
  283.         add(b);
  284.         PropertyChangeListener actionPropertyChangeListener = 
  285.             createActionChangeListener(b);
  286.         a.addPropertyChangeListener(actionPropertyChangeListener);
  287.         return b;
  288.     }
  289.  
  290.     protected PropertyChangeListener createActionChangeListener(JButton b) {
  291.         return new ActionChangedListener(b);
  292.     }
  293.  
  294.     private class ActionChangedListener implements PropertyChangeListener {
  295.         JButton button;
  296.         
  297.         ActionChangedListener(JButton b) {
  298.             super();
  299.             this.button = b;
  300.         }
  301.         public void propertyChange(PropertyChangeEvent e) {
  302.             String propertyName = e.getPropertyName();
  303.             if (e.getPropertyName().equals(Action.NAME)) {
  304.                 String text = (String) e.getNewValue();
  305.                 button.setText(text);
  306.                 button.repaint();
  307.             } else if (propertyName.equals("enabled")) {
  308.                 Boolean enabledState = (Boolean) e.getNewValue();
  309.                 button.setEnabled(enabledState.booleanValue());
  310.                 button.repaint();
  311.             } else if (e.getPropertyName().equals(Action.SMALL_ICON)) {
  312.                 Icon icon = (Icon) e.getNewValue();
  313.                 button.setIcon(icon);
  314.                 button.invalidate();
  315.                 button.repaint();
  316.             } 
  317.         }
  318.     }
  319.  
  320.     /**
  321.      * A toolbar-specific separator. An object with dimension but
  322.      * no contents used to divide buttons on a toolbar into groups.
  323.      * <p>
  324.      * Warning: serialized objects of this class will not be compatible with
  325.      * future swing releases.  The current serialization support is appropriate
  326.      * for short term storage or RMI between Swing1.0 applications.  It will
  327.      * not be possible to load serialized Swing1.0 objects with future releases
  328.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  329.      * baseline for the serialized form of Swing objects.
  330.      */
  331.     public class Separator extends Component {
  332.         /** Create the separator */
  333.         public Separator() {
  334.         }
  335.  
  336.         /** 
  337.          * Return the minimum size for the separator
  338.          *
  339.          * @return the Dimension object containing the separator's
  340.          *         minimum size
  341.          */
  342.         public Dimension getMinimumSize() {
  343.             return new Dimension(10,5);
  344.         }
  345.         /** 
  346.          * Return the maximum size for the separator
  347.          *
  348.          * @return the Dimension object containing the separator's
  349.          *         maximum size
  350.          */
  351.         public Dimension getMaximumSize() {
  352.             return new Dimension(10,5);
  353.         }
  354.         /** 
  355.          * Return the preferred size for the separator
  356.          *
  357.          * @return the Dimension object containing the separator's
  358.          *         preferred size
  359.          */
  360.         public Dimension getPreferredSize() {
  361.             return new Dimension(10,5);
  362.         }
  363.     }
  364.  
  365. /////////////////
  366. // Accessibility support
  367. ////////////////
  368.  
  369.     /**
  370.      * Get the AccessibleContext associated with this JComponent
  371.      *
  372.      * @return the AccessibleContext of this JComponent
  373.      */
  374.     public AccessibleContext getAccessibleContext() {
  375.         if (accessibleContext == null) {
  376.             accessibleContext = new AccessibleJToolBar();
  377.         }
  378.         return accessibleContext;
  379.     }
  380.  
  381.     /**
  382.      * The class used to obtain the accessible role for this object.
  383.      * <p>
  384.      * Warning: serialized objects of this class will not be compatible with
  385.      * future swing releases.  The current serialization support is appropriate
  386.      * for short term storage or RMI between Swing1.0 applications.  It will
  387.      * not be possible to load serialized Swing1.0 objects with future releases
  388.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  389.      * baseline for the serialized form of Swing objects.
  390.      */
  391.     protected class AccessibleJToolBar extends AccessibleJComponent {
  392.  
  393.         /**
  394.          * Get the state of this object.
  395.          *
  396.          * @return an instance of AccessibleStateSet containing the current 
  397.          * state set of the object
  398.          * @see AccessibleState
  399.          */
  400.         public AccessibleStateSet getAccessibleStateSet() {
  401.             AccessibleStateSet states = SwingUtilities.getAccessibleStateSet(JToolBar.this);
  402.             // FIXME:  [[[WDW - need to add orientation from BoxLayout]]]
  403.             // FIXME:  [[[WDW - need to do SELECTABLE if SelectionModel is added]]]
  404.             return states;
  405.         }
  406.     
  407.         /**
  408.          * Get the role of this object.
  409.          *
  410.          * @return an instance of AccessibleRole describing the role of the object
  411.          */
  412.         public AccessibleRole getAccessibleRole() {
  413.             return AccessibleRole.TOOL_BAR;
  414.         }
  415.     } // inner class AccessibleJToolBar
  416. }
  417.